package com.beidasoft.web.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;


/**
 * 日期工具类
 */
public class FzbDateUtil {

    private static final int FILTER_SUNDAY = 1;
    private static final int FILTER_MONDAY = 2;
    private static final int FILTER_TUESDAY = 3;
    private static final int FILTER_WEDNESDAY = 4;
    private static final int FILTER_THURSDAY = 5;
    private static final int FILTER_FRIDAY = 6;
    private static final int FILTER_SATURDAY = 7;

    private static String DF_YMDHMS = "yyyy-MM-dd HH:mm:ss";

    private static String DF_YMDHM = "yyyy-MM-dd HH:mm";
    private static String DF_YMD = "yyyy-MM-dd";

    private static String DATE_FORMAT_NOSPLIT = "yyyyMMddHHmmssSSS";

    /**
     * 将Date类型转换为字符串
     *
     * @param date 日期类型
     * @return 日期字符串
     */
    public static String format(Date date) {
        if (date == null) {
            return "";
        }
        return format(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String format(Calendar date) {
        if (date == null) {
            return "";
        }
        return format(date.getTime(), "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 将Date类型转换为字符串
     *
     * @param date    日期类型
     * @param pattern 字符串格式
     * @return 日期字符串
     */
    public static String format(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        if (pattern == null || pattern.equals("") || pattern.equals("null")) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        return new SimpleDateFormat(pattern).format(date);
    }

    /**
     * 将字符串转换为Date类型
     *
     * @param date 字符串类型
     * @return 日期类型
     */
    public static Date format(String date) {
        return format(date, null, null);
    }

    /**
     * 将字符串转换为Date类型
     *
     * @param date        字符串类型
     * @param pattern     格式
     * @param defaultDate 默认返回日期
     * @return 日期类型
     */
    public static Date format(String date, String pattern, Date defaultDate) {
        if (pattern == null || pattern.equals("") || pattern.equals("null")) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        if (date == null || date.equals("") || date.equals("null")) {
            return defaultDate;
        }
        Date d = null;
        try {
            d = new SimpleDateFormat(pattern).parse(date);
        } catch (ParseException pe) {
        }
        return d;
    }

    public static String getCurrDate() {
        return format(new Date(), "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 格局传入的日期 解析出 该用何种SimpleDateFormat
     *
     * @param dateStr
     * @return
     */
    public static SimpleDateFormat getSimpleDateFormatPattern(String dateStr) {
        SimpleDateFormat format = null;
        if (Pattern.matches("\\d{4}-\\d{1,2}-\\d{1,2}", dateStr)) {
            format = new SimpleDateFormat("yyyy-MM-dd");
            // System.out.println("用的yyyy-MM-dd");
        } else if (Pattern.matches("\\d{4}\\d{2}\\d{2}", dateStr)) {
            format = new SimpleDateFormat("yyyyMMdd");
        } else if (Pattern.matches("\\d{4}年\\d{2}月\\d{2}日", dateStr)) {
            format = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA);
            // System.out.println("用的yyyy年MM月dd日");
        } else if (Pattern.matches("\\d{4}年\\d{1,2}月\\d{1,2}日", dateStr)) {
            format = new SimpleDateFormat("yyyy年M月d日", Locale.CHINA);
        } else if (Pattern.matches("\\d{1,2}\\w{3}\\d{4}", dateStr)) {
            format = new SimpleDateFormat("dMMMyyyy", Locale.ENGLISH);
        } else if (Pattern.matches("\\d{1,2}-\\w{3}-\\d{4}", dateStr)) {
            format = new SimpleDateFormat("d-MMM-yyyy", Locale.ENGLISH);
        } else if (Pattern.matches("\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}:\\d{1,2}", dateStr)) {
            format = new SimpleDateFormat(DF_YMDHM);
        } else if (dateStr.length() > 20) {
            format = new SimpleDateFormat(DF_YMDHMS);
        } else {
            format = new SimpleDateFormat(DF_YMDHMS);
        }
        return format;
    }

    /**
     * 格局传入的日期 解析出 该用何种SimpleDateFormat
     *
     * @param dateStr
     * @return
     * @throws ParseException
     */
    public static Date parseDateByPattern(String dateStr) {
        SimpleDateFormat format = null;
        try {
            if (Pattern.matches("\\d{4}-\\d{2}-\\d{2}", dateStr)) {
                format = new SimpleDateFormat("yyyy-MM-dd");
                return format.parse(dateStr);
            } else if (Pattern.matches("\\d{4}-\\d{1,2}-\\d{1,2}", dateStr)) {
                format = new SimpleDateFormat("yyyy-MM-dd");
                return format.parse(dateStr);
            } else if (Pattern.matches("\\d{4}/\\d{2}/\\d{2}", dateStr)) {
                format = new SimpleDateFormat("yyyy/MM/dd");
                return format.parse(dateStr);
            } else if (Pattern.matches("\\d{4}/\\d{1,2}/\\d{1,2}", dateStr)) {
                format = new SimpleDateFormat("yyyy/MM/dd");
                return format.parse(dateStr);
            } else if (Pattern.matches("\\d{4}\\d{2}\\d{2}", dateStr)) {
                format = new SimpleDateFormat("yyyyMMdd");
                return format.parse(dateStr);
            } else if (Pattern.matches("\\d{4}年\\d{2}月\\d{2}日", dateStr)) {
                format = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA);
                return format.parse(dateStr);
            } else if (Pattern.matches("\\d{4}年\\d{1,2}月\\d{1,2}日", dateStr)) {
                format = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA);
                return format.parse(dateStr);
            } else if (Pattern.matches("\\d{4}/\\d{1,2}/\\d{1,2} \\d{2}:\\d{2}", dateStr)) {
                format = new SimpleDateFormat("yyyy/MM/dd HH:mm", Locale.CHINA);
                return format.parse(dateStr);
            } else if (Pattern.matches("\\d{4}\\.\\d{2}\\.\\d{2}", dateStr)) {
                format = new SimpleDateFormat("yyyy.MM.dd", Locale.CHINA);
                return format.parse(dateStr);
            } else if (Pattern.matches("\\d{4}\\.\\d{1,2}\\.\\d{1,2}", dateStr)) {
                format = new SimpleDateFormat("yyyy.MM.dd", Locale.CHINA);
                return format.parse(dateStr);
            } else if (Pattern.matches("\\d{4}年\\d{2}月", dateStr)) {
                format = new SimpleDateFormat("yyyy年MM月", Locale.CHINA);
                return format.parse(dateStr);
            } else if (Pattern.matches("\\d{4}年\\d{1,2}月", dateStr)) {
                format = new SimpleDateFormat("yyyy年MM月", Locale.CHINA);
                return format.parse(dateStr);
            } else if (Pattern.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}", dateStr)) {
                format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
                return format.parse(dateStr);
            } else if (Pattern.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}", dateStr)) {
                format = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
                return format.parse(dateStr);
            } else if (Pattern.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{3}", dateStr)) {
                format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.CHINA);
                return format.parse(dateStr);
            } else if (Pattern.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{2}", dateStr)) {
                format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SS", Locale.CHINA);
                return format.parse(dateStr);
            } else if (Pattern.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{1}", dateStr)) {
                format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S", Locale.CHINA);
                return format.parse(dateStr);
            } else if (Pattern.matches("\\d{2}:\\d{2}", dateStr)) {
                format = new SimpleDateFormat("HH:mm", Locale.CHINA);
                return format.parse(dateStr);
            }
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * @return long
     * @Author songff
     * @MethodName getMillisecondsRanges
     * @Description //TODO
     * @Date 14:20 2020/9/22
     * @Param [start, end, filters]
     **/
    public static long getMillisecondsRanges(Calendar start, Calendar end, Object filters[]) {
        Calendar tmp = (Calendar) start.clone();
        long mills = 0;// 叠加秒数
        int weekIndex = 0;// 星期几

        while (true) {
            weekIndex = tmp.get(Calendar.DAY_OF_WEEK);// 取出星期

            if (filters != null) {
                boolean ctn = false;
                for (Object filter : filters) {
                    if (filter instanceof Integer) {
                        if (FzbStringUtil.getInteger(filter, 0) == weekIndex) {
                            tmp.set(Calendar.HOUR, 0);
                            tmp.set(Calendar.MINUTE, 0);
                            tmp.set(Calendar.SECOND, 0);
                            tmp.set(Calendar.MILLISECOND, 0);
                            tmp.add(Calendar.DATE, 1);
                            ctn = true;
                            break;
                        }
                    } else if (filter instanceof Calendar) {
                        Calendar t = (Calendar) filter;
                        int year = t.get(Calendar.YEAR);
                        int month = t.get(Calendar.MONTH);
                        int date = t.get(Calendar.DATE);
                        int _year = tmp.get(Calendar.YEAR);
                        int _month = tmp.get(Calendar.MONTH);
                        int _date = tmp.get(Calendar.DATE);
                        if (year == _year && month == _month && date == _date) {
                            tmp.set(Calendar.HOUR, 0);
                            tmp.set(Calendar.MINUTE, 0);
                            tmp.set(Calendar.SECOND, 0);
                            tmp.set(Calendar.MILLISECOND, 0);
                            tmp.add(Calendar.DATE, 1);
                            ctn = true;
                            break;
                        }
                    }
                }
                if (ctn) {
                    continue;
                }
            }

            // 如果当前时间节点还在结束节点之前，并且不在忽略日期范围内的话，则将转到第二天
            if (tmp.before(end)) {
                Calendar c = (Calendar) tmp.clone();
                c.set(Calendar.HOUR, 0);
                c.set(Calendar.MINUTE, 0);
                c.set(Calendar.SECOND, 0);
                c.set(Calendar.MILLISECOND, 0);
                c.add(Calendar.DATE, 1);

                if (c.before(end)) {// 如果当前节点继续在结束节点范围之前，则叠加秒数
                    mills += c.getTime().getTime() - tmp.getTime().getTime();
                    tmp = (Calendar) c.clone();
                } else {
                    mills += (end.getTime().getTime() - tmp.getTime().getTime());
                    break;
                }
            }
        }
        return mills;
    }

    /**
     * 获取逝去的时间描述
     *
     * @param start
     * @param end
     * @return
     */
    public static String getPassedTimeDesc(Calendar start, Calendar end) {
        if (start == null) {
            return "";
        }
        if (end == null) {
            end = Calendar.getInstance();
        }
        long milliSeconds = end.getTimeInMillis() - start.getTimeInMillis();
        return getTimeMilisecondDesc(milliSeconds);
    }

    /**
     * 获取时间日期
     *
     * @param milliSeconds
     * @return
     */
    public static String getTimeMilisecondDesc(long milliSeconds) {
        long days = milliSeconds / (1000 * 60 * 60 * 24);
        milliSeconds = milliSeconds - (days * 24 * 60 * 60 * 1000);
        long hours = milliSeconds / (1000 * 60 * 60);
        milliSeconds = milliSeconds - (hours * 60 * 60 * 1000);
        long minutes = milliSeconds / (1000 * 60);
        milliSeconds = milliSeconds - (minutes * 60 * 1000);
        long seconds = milliSeconds / (1000);

        StringBuffer sb = new StringBuffer();
        if (days != 0) {
            sb.append(days + "天");
        }
        if (hours != 0) {
            sb.append(hours + "小时");
        }
        if (minutes != 0) {
            sb.append(minutes + "分钟");
        }
        if (seconds != 0) {
            sb.append(seconds + "秒");
        }
        return sb.toString();
    }

    /**
     * 取得当前日期所在周的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar c = clearDate(date, 4);
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
        return c.getTime();
    }

    /**
     * 取得当前日期所在周的最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar c = clearDate(date, 4);
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 000);
        return c.getTime();
    }

    /**
     * 取得指定日期的当周的起始时间
     *
     * @param date
     * @return
     */
    public static Date[] getWeekLimit(Date date) throws Exception {
        Date date1 = getFirstDayOfWeek(date);
        Date date2 = getLastDayOfWeek(date);
        return new Date[]{date1, date2};
    }

    /**
     * 取得指定日期的当月起始时间
     *
     * @param date
     * @return
     */
    public static Date[] getMonthLimit(Date date) throws Exception {
        Calendar cal = clearDate(date, 5);
        cal.set(Calendar.DATE, 1);
        Date date1 = cal.getTime();

        cal.add(Calendar.MONTH, 1);
        cal.add(Calendar.SECOND, -1);
        Date date2 = cal.getTime();

        return new Date[]{date1, date2};
    }

    /**
     * 取得指定日期的当年起始时间
     *
     * @param date
     * @return
     */
    public static Date[] getYearLimit(Date date) throws Exception {
        Calendar cal = clearDate(date, 6);
        cal.set(Calendar.MONTH, 0);
        cal.set(Calendar.DATE, 1);
        Date date1 = cal.getTime();

        cal.add(Calendar.YEAR, 1);
        cal.add(Calendar.SECOND, -1);
        Date date2 = cal.getTime();

        return new Date[]{date1, date2};
    }

    /**
     * 取得指定日期当月的起始时间串
     *
     * @param date
     * @return
     */
    public static String[] getMonthLimitStr(Date date) throws Exception {
        Date[] rtDateArray = getMonthLimit(date);
        return new String[]{getDateTimeStr(rtDateArray[0]), getDateTimeStr(rtDateArray[1])};
    }

    /**
     * 取得指定日期当年的起始时间串
     *
     * @param date
     * @return
     */
    public static String[] getYearLimitStr(Date date) throws Exception {
        Date[] rtDateArray = getYearLimit(date);
        return new String[]{getDateTimeStr(rtDateArray[0]), getDateTimeStr(rtDateArray[1])};
    }

    /**
     * @return int
     * @Author songff
     * @MethodName getDaySpan
     * @Description //TODO  取得天数间隔
     * @Date 14:06 2020/9/22
     * @Param [toDateStr]
     **/
    public static int getDaySpan(String toDateStr) throws Exception {
        return (int) ((parseDate(toDateStr).getTime() - new Date().getTime()) / ConstantUtil.TEE_D);
    }

    /**
     * 取得天数间隔
     *
     * @param toDate
     * @param toDate
     * @return
     */
    public static int getDaySpan(Date toDate) {
        return (int) ((toDate.getTime() - new Date().getTime()) / ConstantUtil.TEE_D);
    }

    /**
     * 取得天数间隔
     *
     * @param fromDate
     * @param toDate
     * @return
     */
    public static int getDaySpan(Date fromDate, Date toDate) {
        return (int) ((toDate.getTime() - fromDate.getTime()) / ConstantUtil.TEE_D);
    }

    /**
     * 取得天数间隔 毫秒
     *
     * @param fromDate
     * @param toDate
     * @return
     */
    public static int getDaySpanTimeInMillis(long fromDate, long toDate) {
        return (int) ((fromDate - toDate) / ConstantUtil.TEE_D);
    }

    /**
     * 取得前一天的时间
     *
     * @param dateStr
     * @return
     */
    public static Date getDayBefore(String dateStr, int dayCnt) throws Exception {
        return getDayBefore(parseDate(dateStr), dayCnt);
    }

    /**
     * 取得前一天的时间
     *
     * @param date
     * @return
     */
    public static Date getDayBefore(Date date, int dayCnt) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.DATE, 0 - dayCnt);
        return cal.getTime();
    }

    /**
     * 取得后一天的时间字
     *
     * @param dateStr
     * @return
     */
    public static Date getDayAfter(String dateStr, int dayCnt) throws Exception {
        return getDayAfter(parseDate(dateStr), dayCnt);
    }

    /**
     * 取得后一天的时间
     *
     * @param date
     * @return
     */
    public static Date getDayAfter(Date date, int dayCnt) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.DATE, dayCnt);
        return cal.getTime();
    }

    /**
     * 取得后N天的时间的所有日期list
     *
     * @param date
     * @return
     */
    public static List<Date> getDayListAfter(Date date, int dayCnt) {
        List<Date> list = new ArrayList<Date>();
        GregorianCalendar cal = new GregorianCalendar();
        for (int i = 1; i <= dayCnt; i++) {
            cal.setTime(date);
            cal.add(Calendar.DATE, i);
            list.add(cal.getTime());
        }
        return list;
    }

    /**
     * 取得指定天数差的时间字
     *
     * @param dateStr
     * @return
     */
    public static Date getDayDiff(String dateStr, int dayCnt) throws Exception {
        return getDayDiff(parseDate(dateStr), dayCnt);
    }

    /**
     * 取得指定天数差的时间
     *
     * @param date
     * @return
     */
    public static Date getDayDiff(Date date, int dayCnt) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.DATE, dayCnt);
        return cal.getTime();
    }

    /**
     * 取得前一天的时间字
     *
     * @param dateStr
     * @return
     */
    public static Date getYestday(String dateStr) throws Exception {
        return getYestday(parseDate(dateStr));
    }

    /**
     * 取得前一天的时间
     *
     * @param date
     * @return
     */
    public static Date getYestday(Date date) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.DATE, -1);
        return cal.getTime();
    }

    /**
     * 取得前一天的时间字符串
     *
     * @param dateStr
     * @return
     */
    public static String getYestdayStr(String dateStr) throws Exception {
        return getYestdayStr(parseDate(dateStr));
    }

    /**
     * 取得前一天的时间字符串
     *
     * @param date
     * @return
     */
    public static String getYestdayStr(Date date) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.DATE, -1);
        return getDateTimeStr(cal.getTime());
    }

    /**
     * Date清零
     *
     * @param date
     * @param clearNum 1=毫秒, 2=秒, 3=分钟, 4=小时, 5=天, 6=月份
     * @return
     */
    private static Calendar clearDate(Date date, int clearNum) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        // 毫秒
        if (clearNum > 0) {
            cal.set(Calendar.MILLISECOND, 0);
        }
        // 秒
        if (clearNum > 1) {
            cal.set(Calendar.SECOND, 0);
        }
        // 分钟
        if (clearNum > 2) {
            cal.set(Calendar.MINUTE, 0);
        }
        // 小时
        if (clearNum > 3) {
            cal.set(Calendar.HOUR_OF_DAY, 0);
        }
        // 天
        if (clearNum > 4) {
            cal.set(Calendar.DATE, 0);
        }
        // 月份
        if (clearNum > 5) {
            cal.set(Calendar.MONTH, 0);
        }
        return cal;
    }

    /**
     * 把字符串转化为Date
     *
     * @param dateStr
     * @return
     */
    public static Date parseDate(String formatStr, String dateStr) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        return format.parse(dateStr);
    }

    public static Calendar parseCalendar(String formatStr, String dateStr) {
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(parseDate(formatStr, dateStr));
        } catch (Exception e) {
            return null;
        }
        return c;
    }

    public static Calendar parseCalendar(String dateStr) {
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(parseDateByPattern(dateStr));
        } catch (Exception e) {
            return null;
        }
        return c;
    }

    /**
     * 把字符串转化为Date
     *
     * @param dateStr
     * @return
     */
    public static Date parseDate(String dateStr) {
        if (dateStr == null || "".equals(dateStr)) {
            return null;
        }
        return parseDateByPattern(dateStr);
    }

    /**
     * 取得的时间串，格式为 yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getDateTimeStr(Date date) {
        if (date == null) {
            return getCurDateTimeStr();
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat(DF_YMDHMS);
        return dateFormat.format(date);
    }

    /**
     * 取得当前的时间，格式为 yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getCurDateTimeStr() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DF_YMDHMS);
        return dateFormat.format(new Date());
    }

    /**
     * @param year
     * @return
     * @return计算一年的最大周
     * @author syl
     * @date 2014-1-11
     */
    public static int getMaxWeekOfYear(int year) {
        Calendar c = new GregorianCalendar();
        c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
        return getWeekOfYear(c.getTime());
    }

    /**
     * 获取日期当前周
     *
     * @param date
     * @return
     * @author syl
     * @date 2014-1-11
     */
    public static int getWeekOfYear(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setMinimalDaysInFirstWeek(7);
        c.setTime(date);
        return c.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 参数说明 返回一个Calendar数组，长度7 获取一周的所有日期 7议案
     *
     * @param year    年分 例如 2014 i
     * @param weeknum 第几周 例如33
     * @return
     * @author syl
     * @date 2014-1-11
     */
    public static Calendar[] getStartToEndDate(int year, int weeknum) {

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.WEEK_OF_YEAR, weeknum);
        int nw = cal.get(Calendar.DAY_OF_WEEK);
        Calendar start = (Calendar) cal.clone();
        Calendar date2 = (Calendar) cal.clone();
        Calendar date3 = (Calendar) cal.clone();
        Calendar date4 = (Calendar) cal.clone();
        Calendar date5 = (Calendar) cal.clone();
        Calendar date6 = (Calendar) cal.clone();
        Calendar end = (Calendar) cal.clone();

        start.add(Calendar.DATE, 1 - nw + 1);
        FzbUtility.getMinTimeOfDayCalendar(start);
        date2.add(Calendar.DATE, 1 - nw + 2);
        FzbUtility.getMinTimeOfDayCalendar(date2);
        date3.add(Calendar.DATE, 1 - nw + 3);
        FzbUtility.getMinTimeOfDayCalendar(date3);
        date4.add(Calendar.DATE, 1 - nw + 4);
        FzbUtility.getMinTimeOfDayCalendar(date4);
        date5.add(Calendar.DATE, 1 - nw + 5);
        FzbUtility.getMinTimeOfDayCalendar(date5);
        date6.add(Calendar.DATE, 1 - nw + 6);
        FzbUtility.getMinTimeOfDayCalendar(date6);
        end.add(Calendar.DATE, 7 - nw + 1);
        FzbUtility.getMinTimeOfDayCalendar(end);
        Calendar[] darr = {start, date2, date3, date4, date5, date6, end};
        return darr;
    }

    /**
     * 参数说明 返回一个Calendar数组，长度为2 分别是开始日期和结束日期
     *
     * @param year    年分 例如 2014 i
     * @param weeknum 第几周 例如33
     * @return
     * @author syl
     * @date 2014-1-11
     */
    public static Calendar[] getStartAndEndDate(int year, int weeknum) {

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.WEEK_OF_YEAR, weeknum);
        int nw = cal.get(Calendar.DAY_OF_WEEK);
        Calendar start = (Calendar) cal.clone();
        Calendar end = (Calendar) cal.clone();
        start.add(Calendar.DATE, 1 - nw + 1);
        end.add(Calendar.DATE, 7 - nw + 1);
        Calendar[] darr = {start, end};
        return darr;
    }

    /**
     * 获取当期日期的一周 开始可结束日期 返回一个Calendar数组，长度为2 分别是开始日期和结束日期
     *
     * @return
     * @author syl
     * @date 2014-1-11
     */
    public static Calendar[] getCurrStartEndDate() {
        Calendar cal = Calendar.getInstance();
        // 向后推一天（从星期一到周末）
        cal.add(Calendar.DATE, -1);
        int nw = cal.get(Calendar.DAY_OF_WEEK);
        Calendar start = (Calendar) cal.clone();
        Calendar end = (Calendar) cal.clone();
        start.add(Calendar.DATE, 1 - nw + 1);
        end.add(Calendar.DATE, 7 - nw + 1);
        Calendar[] darr = {start, end};
        return darr;
    }

    /**
     * 获取当期日期的一周 开始至结束日期 返回一个Calendar数组，长度为7
     *
     * @return
     * @author syl
     * @date 2014-1-11
     */
    public static Calendar[] getCurrStartAndEndOfWeek() {
        Calendar cal = Calendar.getInstance();
        // 向后推一天（从星期一到周末）
        cal.add(Calendar.DATE, -1);
        int nw = cal.get(Calendar.DAY_OF_WEEK);
        Calendar start = (Calendar) cal.clone();
        start.add(Calendar.DATE, 1 - nw + 1);
        Calendar tuesday = (Calendar) cal.clone();// 周二
        tuesday.add(Calendar.DATE, 2 - nw + 1);
        Calendar wednesday = (Calendar) cal.clone();// 周三
        wednesday.add(Calendar.DATE, 3 - nw + 1);
        Calendar thursday = (Calendar) cal.clone();// 周四
        thursday.add(Calendar.DATE, 4 - nw + 1);
        Calendar friday = (Calendar) cal.clone();// 周五
        friday.add(Calendar.DATE, 5 - nw + 1);
        Calendar saturday = (Calendar) cal.clone();// 周六
        saturday.add(Calendar.DATE, 6 - nw + 1);
        Calendar end = (Calendar) cal.clone();
        end.add(Calendar.DATE, 7 - nw + 1);
        Calendar[] darr = {start, tuesday, wednesday, thursday, friday, saturday, end};
        return darr;
    }

    /**
     * 获取当期日期的一周 开始至结束日期 返回一个Calendar数组，长度为7
     *
     * @return
     * @author syl
     * @date 2014-1-11
     */
    public static Calendar[] getStartAndEndOfWeekByDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 向后推一天（从星期一到周末）
        cal.add(Calendar.DATE, -1);
        int nw = cal.get(Calendar.DAY_OF_WEEK);
        Calendar start = (Calendar) cal.clone();
        start.add(Calendar.DATE, 1 - nw + 1);
        Calendar tuesday = (Calendar) cal.clone();// 周二
        tuesday.add(Calendar.DATE, 2 - nw + 1);
        Calendar wednesday = (Calendar) cal.clone();// 周三
        wednesday.add(Calendar.DATE, 3 - nw + 1);
        Calendar thursday = (Calendar) cal.clone();// 周四
        thursday.add(Calendar.DATE, 4 - nw + 1);
        Calendar friday = (Calendar) cal.clone();// 周五
        friday.add(Calendar.DATE, 5 - nw + 1);
        Calendar saturday = (Calendar) cal.clone();// 周六
        saturday.add(Calendar.DATE, 6 - nw + 1);
        Calendar end = (Calendar) cal.clone();
        end.add(Calendar.DATE, 7 - nw + 1);
        Calendar[] darr = {start, tuesday, wednesday, thursday, friday, saturday, end};
        return darr;
    }

    /**
     * 取两个日程的之间所有天 ---数组
     *
     * @param fromDate
     * @param toDate
     * @return
     * @author syl
     * @date 2014-1-21
     */
    public static Calendar[] getDaySpanCalendar(Date fromDate, Date toDate) {
        int dayDay = FzbUtility.getDaySpan(fromDate, toDate);
        Calendar[] calDate = new Calendar[dayDay];
        Calendar cal = Calendar.getInstance();
        cal.setTime(fromDate);
        FzbUtility.getMinTimeOfDayCalendar(cal);
        calDate[0] = cal;
        for (int i = 1; i < dayDay; i++) {
            Calendar calTemp = Calendar.getInstance();
            calTemp.setTime(fromDate);
            calTemp.add(Calendar.DATE, +i);
            calDate[i] = calTemp;
        }
        return calDate;
    }

    /**
     * 获取当前日期是周几 返回 1,2,3,4,5,6,7
     *
     * @param dateStr
     * @return
     */
    public static String getWeekOfDate(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = null;
        String[] weekDays = {"7", "1", "2", "3", "4", "5", "6"};
        int w = 0;
        try {
            dt = sdf.parse(dateStr);
            Calendar cal = Calendar.getInstance();
            cal.setTime(dt);
            w = cal.get(Calendar.DAY_OF_WEEK) - 1;
            if (w < 0) {
                w = 0;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return weekDays[w];
    }

    /**
     * Date 转 LocalDateTime
     *
     * @return
     */
    public static LocalDateTime convert(Date date) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
    }

    /**
     * LocalDateTime 转 Date
     *
     * @return
     */
    public static Date convert(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 获取昨天日期（yyyy-MM-dd）
     * @return
     */
    public static String getYesterdayStr() {
        return getYesterdayStr(new Date());
    }

    /**
     * 获取前一天日期（yyyy-MM-dd）
     * @param date 日期
     * @return
     */
    public static String getYesterdayStr(Date date) {
        return format(getDayAfter(date,-1),DF_YMD);
    }


}
